// var boxWidth = 700;//盒子的宽度
var colNum = 5;//每行显示个数
var grooveWidth = 10;//槽宽
Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    picWidth: "",
    grooveWidth: "",
    picBoxWidth: "",
    picBoxHeight: "",
    animationData: {},
    imgHiddenData: {},
    tips: "找到躲起来的福利吧！",
    challengeNum: 1,//挑战次数
    imgHidden: false,
    defaultHidden: true,
    defaulttrue: true,
    defaultImg: "../../images/head.jpg",
    changeImg: "../../images/changeImg.png",
    idx: "",
    picAnimation: {},
    randomNum: "",
    hongbaoImg: "",
    wxmlData: [],
    picboxbg: false,
    givUp: false,
    tryAgain: true,
    rotateY: '',
    rotatedelay: ''
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth - 20,
          winHeight: res.windowHeight
        });
      }
    });
    that.random();
    setTimeout(function () {
      that.shadeAnimate();
    }, 2000)
    setTimeout(function () {
      that.setData({
        rotatedelay: 'rotatedelay',
        defaultHidden: false
      })
    }, 3000)
    that.createElement();
    setTimeout(function () {
      that.exchangeLocation(4);
    }, 4000)
  },
  createElement() {
    var that = this;
    var imgWidth = (that.data.winWidth / colNum);//图片的宽度
    var wxmlData = [];
    for (var i = 0; i < colNum; i++) {
      for (var j = 0; j < colNum; j++) {
        var top = i * imgWidth;
        var left = j * imgWidth;
        var arr = [top, left];
        wxmlData.push(arr)
      }
    }
    this.setData({
      picWidth: imgWidth - 10,
      grooveWidth: grooveWidth,//槽宽
      picBoxWidth: that.data.winWidth,
      picBoxHeight: that.data.winWidth,
      colNum: colNum,//每行显示的个数
      wxmlData: wxmlData
    })
  },
  onReady: function () {

  },
  onShow: function () {
    
  },//再试一次
  tryAgain() {
    var that = this;
    that.setData({
      tryAgain: true,
      challengeNum: that.data.challengeNum + 1
    })
    setTimeout(function () {
      that.shadeAnimate();
    }, 1000)
    setTimeout(function () {
      that.setData({
        rotateY: 'rotateY',
        rotatedelay: 'rotatedelay',
        givUp: false,
        tips: "找到躲起来的福利吧！",
        defaultHidden: false,
        imgHidden: true,
        defaulttrue: false,
      })
    }, 2000)
    setTimeout(function () {
      that.exchangeLocation(4);
    }, 3000)
  },//放弃挑战
  givUp() {

  },
  random() {
    var num = Math.floor(Math.random() * (colNum * colNum) + 1);
    this.setData({ randomNum: num - 1 })
  },//点击当前翻转
  bindcurrent: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.index;
    that.setData({
      idx: idx,
      rotatedelay: 'rotateY',
      rotateY: '',
      picboxbg: false,
      givUp: true,
      defaultHidden: false,
      imgHidden: false,
      defaulttrue: true,
    });
    setTimeout(function () {
      that.setData({
        rotateY: '',
        rotatedelay: '',
        defaultHidden: true,
        imgHidden: false,
        defaulttrue: true,
      })
    }, 1000)
    if (that.data.idx == that.data.randomNum) {
      that.setData({
        tips: "恭喜您抢到红包啦！",
        tryAgain: true,
      })
      setTimeout(function () {

      }, 1000)
    } else {
      that.setData({
        tips: "没有中呢，注意力要集中哦！",
        tryAgain: false,
      })
    }
  },//抖动
  shadeAnimate() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 1,
      timingFunction: 'linear',
    })
    that.animation = animation
    // for (var i = 0; i <= 3; i++) {
      animation.translateX(0).step()
    // }
    that.setData({
      imgHiddenData: animation.export()
    })
  },//交换位置
  exchangeLocation(max){
    var k = 1;
    var that=this;
    var wxmlData = that.data.wxmlData;
    var timer = setInterval(function () {
      if (k >= max) {
        clearInterval(timer)
        that.setData({
          picboxbg: true,
        })
      }
      that.setData({
        wxmlData: wxmlData.sort(function () { return 0.5 - Math.random() }),
      })
      ++k
    }, 1000)
  }
})